package com.zhongxi.guoyuan.view;

import android.content.SharedPreferences;
import android.os.Handler;

import com.google.gson.Gson;
import com.zhongxi.guoyuan.R;
import com.zhongxi.guoyuan.application.MyApplication;
import com.zhongxi.guoyuan.base.BaseActivity;
import com.zhongxi.guoyuan.entity.BaseResponse;
import com.zhongxi.guoyuan.url.Url;
import com.zhongxi.guoyuan.utils.RegularTool;

import org.json.JSONObject;

import java.util.Calendar;

public class WelcomeActivity extends BaseActivity {

    @Override
    protected void initView() {
        super.initView();
        setContentView(R.layout.activity_welcome);

        SharedPreferences sharedPrefrences = getSharedPreferences(Url.APP_SP, MODE_PRIVATE);
        sharedPrefrences.edit().putString("now", Calendar.getInstance().getTimeInMillis() +"").commit();
        sharedPrefrences.edit().putString("sk", RegularTool.getRandomString(8)).commit();

    }

    @Override
    protected void initData() {
        super.initData();
        getServer(Url.WELCOME, null, "", false, null, "get");
    }

    @Override
    protected void processSuccessResult(String response) {
        super.processSuccessResult(response);
        try{
            JSONObject obj = new JSONObject(response);
            String express = obj.getString("expressCode");
            SharedPreferences sharedPrefrences = getSharedPreferences(Url.APP_SP, MODE_PRIVATE);
            sharedPrefrences.edit().putString("logistics", express).commit();
        }catch (Exception e){
            e.printStackTrace();
        }
        Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                startNextActivity();
            }
        }, 1000);

    }

    @Override
    protected void ProcessFailResult(String response) {
        super.ProcessFailResult(response);
        Gson gson = new Gson();
        BaseResponse result = gson.fromJson(response, BaseResponse.class);
        if(null!=result.getSafeobj()&&!"".equals(result.getSafeobj())){
            MyApplication.getInstance().setSpValue(result.getSafeobj());
        }
    }

    @Override
    protected void initEvent() {
        super.initEvent();
    }


    private void startNextActivity() {
        startAct(MainHomeActivity.class);
        finish();
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
    }
}
